import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import 'package:sainbainu/common/values/colors.dart';

enum ImageErrorType {
  normal,
  avatar;

  Widget get image {
    switch (this) {
      case ImageErrorType.normal:
        return Image.asset(
          'assets/images/error-img.png',
          width: 30,
          height: 30,
        );
      case ImageErrorType.avatar:
        return Image.asset(
          'assets/images/avatar.png',
          fit: BoxFit.cover,
        );
    }
  }
}

class CustomImage extends StatelessWidget {
  final String url;
  final BoxFit fit;
  final double? width;
  final double? height;
  final ImageErrorType errorType;

  const CustomImage({
    super.key,
    required this.url,
    this.fit = BoxFit.cover,
    this.width,
    this.height,
    this.errorType = ImageErrorType.normal,
  });

  @override
  Widget build(BuildContext context) {
    return CachedNetworkImage(
      imageUrl: url,
      fit: fit,
      width: width,
      height: height,
      errorWidget: (context, url, error) => Container(
        color: AppColors.page,
        alignment: Alignment.center,
        child: errorType.image,
      ),
      placeholder: (context, url) {
        return Container(
          color: AppColors.page,
          alignment: Alignment.center,
          child: LoadingAnimationWidget.discreteCircle(
            secondRingColor: Colors.redAccent,
            thirdRingColor: Colors.blueAccent,
            color: Colors.amber,
            size: 30,
          ),
        );
      },
    );
  }
}
